<template>
  <el-card>
    <el-form :disabled="isDisable" :inline="true"  class="demo-form-inline category-selector">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" placeholder="请选择">
          <!-- value属性中为什么要存一个id？因为当前这个下拉框选中的value与v-model是对应的关系，只要有选中,value值会最终存储到v-model对应的属性中，而我们的三级联动中，二级数据的获取是根据一级分类数据中的id来获取的 -->
          <el-option :label="c1.name" v-for="c1 in categoryStore.category1List" :key="c1.id" :value="c1.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择">
          <el-option :label="c2.name" v-for="c2 in categoryStore.category2List" :key="c2.id" :value="c2.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择">
          <el-option :label="c3.name" v-for="c3 in categoryStore.category3List" :key="c3.id" :value="c3.id" />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name:'CategorySelector',
};
</script>
<script lang="ts" setup>
import { computed, onMounted, reactive } from 'vue'
import { useCategoryListStore } from '@/stores/categoryList'

defineProps(['isDisable'])

// 获取对应的store对象
const categoryStore = useCategoryListStore()


// 计算属性一级导航
const category1Id = computed({
  get() {
    return categoryStore.category1Id as number
  },
  set(id: number) {
    // console.log('一级导航分类被选中了...')
    // categoryStore.category1Id = id

    categoryStore.getCategory2ListData(id)
  }
})

// 计算属性之二级导航
const category2Id = computed({
  get() {
    return categoryStore.category2Id as number
  },
  set(id:number) {
   categoryStore.getCategory3ListData(id)
  }
})

// 计算属性之三级导航
const category3Id = computed({
  get() {
    return categoryStore.category3Id as number
  },
  set(id:number) {
    // console.log('三级导航选中了...',id)
    categoryStore.setCategory3Id(id) 
  }
})

onMounted(() => {
  categoryStore.getCategory1ListData()
})

const onSubmit = () => {
  console.log('submit!')
}
</script>

<style scoped lang="scss">
.category-selector{
  .el-form-item {
    margin-bottom:0;
  }
} 
 
</style>